package com.samphanie.lib.common.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.samphanie.lib.common.R;

public class EmptyView extends LinearLayout {

    private ImageView icon;
    private TextView text;
    private Button action;

    public EmptyView(Context context) {
        this(context, null);
    }

    public EmptyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmptyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public EmptyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        LayoutInflater.from(context).inflate(R.layout.layout_empty_view, this, true);


        icon = findViewById(R.id.empty_icon);
        text = findViewById(R.id.empty_text);
        action = findViewById(R.id.empty_action);
    }

    public ImageView getIcon() {
        return icon;
    }

    public void setIcon(@DrawableRes int iconRes) {
        this.icon.setImageResource(iconRes);
    }

    public TextView getText() {
        return text;
    }

    public void setText(String text) {
        if (TextUtils.isEmpty(text)) {
            this.text.setVisibility(View.GONE);
        } else {
            this.text.setText(text);
            this.text.setVisibility(View.VISIBLE);
        }
    }

    public Button getAction() {
        return action;
    }

    public void setAction(String text, View.OnClickListener listener) {
        if (TextUtils.isEmpty(text)) {
            this.action.setVisibility(View.GONE);
        } else {
            this.action.setText(text);
            this.action.setVisibility(View.VISIBLE);
            this.action.setOnClickListener(listener);
        }
    }
}
